package com.tiemei.demo.client.rc;

import static com.tiemei.demo.util.Print.p;

import java.net.URLEncoder;
import java.util.Collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.taobao.hsf.hsfunit.HSFEasyStarter;
import com.taobao.hsf.hsfunit.util.ServiceUtil;
import com.taobao.ratecenter.domain.dataobject.EvaluateInfo;
import com.taobao.ratecenter.domain.dataobject.FeedRateBatchQueryDO;
import com.taobao.ratecenter.domain.dataobject.FeedRateDO;
import com.taobao.ratecenter.domain.dataobject.result.RateResult;
import com.taobao.ratecenter.service.RateCenterEvaluateService;
import com.taobao.ratecenter.service.RateCenterQueryService;
import com.taobao.ratecenter.service.RateCenterReadService;

public class RcClient {
	// feed
	private static RateCenterReadService rateCenterReadService;
	private static RateCenterQueryService rateCenterQueryService;
	// dsr
	private static RateCenterEvaluateService rateCenterEvaluateService;

	private static int feedbackContentLength = 20;
	// init
	static {
		HSFEasyStarter.start("d:/hsf/release", "");
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hsf-udc.xml");
		rateCenterReadService = (RateCenterReadService) ctx.getBean("rateCenterReadService");
		rateCenterQueryService = (RateCenterQueryService) ctx.getBean("rateCenterQueryService");
		rateCenterEvaluateService = (RateCenterEvaluateService) ctx.getBean("rateCenterEvaluateService");
		ServiceUtil.waitServiceReady(rateCenterReadService, rateCenterQueryService);// 不是必须，因为运行很快，所以要在configServer推地址下来前检测地址是否推下來，可以自己sleep(500)
		p("server client ready!-------------------");
	};

	public static void getFeedback(Long auctionId, Long sellerId) {
		FeedRateBatchQueryDO query = new FeedRateBatchQueryDO();
		query.setOrderType("sort_weight");// rc语义筛选
		query.setRaterType(0); // 买家评论，另利于提升查询速度
		query.setAuctionNumId(auctionId);
		query.setRatedId(sellerId);
		query.setReceivedOrSend(0); // 接受评论
		query.setSource("p4p-trident");
		query.setOrderBy("desc"); // 倒序输出
		query.setPageSize(5);
		query.setCurrentPageNum(1);
		RateResult<FeedRateDO> result = rateCenterQueryService.queryFeedRateListV2(query).check();
		if (result.isSuccess()) { // 能查询到数据
			Collection<FeedRateDO> dataList = result.getDataList();
			for (FeedRateDO feed : dataList) { // 筛选出评论内容长度大于20的一个
				p("feedback: " + feed.getFeedback() + " feednick: " + feed.getRatedUserNick());
//				if (feed.getFeedback() != null && feed.getFeedback().length() > feedbackContentLength) { // 可能某个数据段为null
//					break;
//				}
			}
		} else { // 无符合条件的数据
			p("没有查询到数据 auctionId=" + auctionId + " ,sellerId"+sellerId);
		}
	}
	
	/**
	 * TODOD 部分字段还没加上
	 * @param userId
	 */
	public static void getDsr(Long userId) {
		EvaluateInfo res = rateCenterEvaluateService.getEvaluateInfo(userId);
		if(res != null) { // rc存在该userId的数据
			String describeGap = "0";
			if(res.isMerchandisGapUp()) {
				describeGap = res.getMerchandisGapString();
			} else if(res.isMerchandisGapBottom()) {
				describeGap = "-" + res.getMerchandisGapString();
			}
			String serviceGap = "0";
			if(res.isServiceGapUp()) {
				serviceGap = res.getServiceGapString();
			} else if(res.isServiceGapBottom()) {
				serviceGap = "-" + res.getServiceGapString();
			}
//			p("ind: "+res.getInd()+ " dsrDescribe: "+res.getMerchandisAvgScoreString(1)+ " dsrDescribeGap: "+describeGap);
			
			
//			/*
//			 * 服务态度
//			 */
//			dsrFeatureDo.setDsrService(res.getServiceAvgScoreString(1));
//			String serviceGap = "0";
//			if(res.isServiceGapUp()) {
//				serviceGap = res.getServiceGapString();
//			} else if(res.isServiceGapBottom()) {
//				serviceGap = "-" + res.getServiceGapString();
//			}
//			dsrFeatureDo.setDsrServiceGap(serviceGap);
//			/*
//			 * 发货速度
//			 */
//			dsrFeatureDo.setDsrDeliver(res.getConsignmentAvgScoreString(1));
//			String deliverGap = "0";
//			if(res.isConsignmentGapUp()) {
//				deliverGap = res.getConsignmentGapString();
//			} else if(res.isConsignmentGapBottom()) {
//				deliverGap = "-" + res.getConsignmentGapString();
//			}
//			dsrFeatureDo.setDsrDeliverGap(deliverGap);
//			return dsrFeatureDo;
//		} else {
//			log.error(errorPrefix + " userId = " + userId + ", rc return dsr is null");
//			return dsrFeatureDo;
		}
	}

	public static void main(String args[]) {
//		getFeedback(1500017004124L, 2050919495L);
		getFeedback(1500017004124L, 2050919495L);
	}

}
